import { BoxCollider2D, Collider2D, Contact2DType, director, EventTouch, IPhysics2DContact, MotionStreak, UITransform, Vec3 } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { gameFruitMainFruitGroup } from './gameFruitMainFruitGroup';

const { ccclass, property } = _decorator;

/**
 * 水果忍者-主界面脚本
 */
@ccclass('gameFruitMain')
export class gameFruitMain extends Component {

    @property(Node)
    private fruitGruop: Node = null;

    /**
     * 水果刀-拖影
     */
    @property(Node)
    private knifeNode: Node = null;
    private knifeMotionS: MotionStreak;

    start() {
        console.log('start');
        this.init();
        this.knifeMove();
    }

    init() {
        console.log('init');

        this.fruitGruop.getComponent(gameFruitMainFruitGroup).createFruitList();
    }

    onLoad() {

        this.knifeMotionS = this.knifeNode.getComponent(MotionStreak);

        console.log("gameFruitMain onLoad")
    }

    update(deltaTime: number) {

    }

    /**
     * 点击返回按钮
     */
    clickBackBtn() {
        director.loadScene('gameFruitStart');
    }

    knifeMove() {
        this.node.on(Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
    }

    touchMoveEvent(e: EventTouch) {
        let transform = this.node.getComponent(UITransform);
        let pos = transform.convertToNodeSpaceAR(new Vec3(e.getLocationX(), e.getLocationY()));
        this.knifeNode.setPosition(pos);

        //console.log("touchMoveEvent:{}", e);
    }

    touchStartEvent(e: EventTouch) {
        let transform = this.node.getComponent(UITransform);
        let pos = transform.convertToNodeSpaceAR(new Vec3(e.getLocationX(), e.getLocationY()));
        this.knifeNode.setPosition(pos);
        this.knifeMotionS.reset();

        //console.log("touchStartEvent");
    }

    onCollisionEnter(other, self) {
        console.log("onCollisionEnter other:{},self:{}", other, self)
    }
}


